// @Title api[简述该包的主要作用]
// @Description
// @Author  cnHuaShao
// @since v0.0.1
// @Version v1.0.0
// @Date 2024/5/22 23:51
// @Update 2024/5/22 23:51
package api

import (
	"FinancialManagement/src/base/mess"
	"FinancialManagement/src/base/util"
	"FinancialManagement/src/business/customer"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// GetCustomerListAll
// @Description: 获取所有客户信息，不分页
// @param c
func GetCustomerListAll(c *gin.Context) {
	provinceId := util.ToInt(c.DefaultPostForm("provinceId", "0"))     // 获取省份ID
	cityId := util.ToInt(c.DefaultPostForm("cityId", "0"))             // 获取城市ID
	customerType := util.ToInt(c.DefaultPostForm("customerType", "0")) // 客户类型
	customerList, err := customer.GetCustomerListAll(provinceId, cityId, customerType)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005007, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(customerList))
}

// GetCustomerList
// @Description: 获取所有客户信息，带分页
// @param c
func GetCustomerList(c *gin.Context) {
	name := c.DefaultPostForm("name", "")                              // 获取客户名称
	provinceId := util.ToInt(c.DefaultPostForm("provinceId", "0"))     // 获取省份ID
	cityId := util.ToInt(c.DefaultPostForm("cityId", "0"))             // 获取城市ID
	pageSize := util.ToInt(c.DefaultPostForm("pageSize", "10"))        // 显示行数
	pageNumber := util.ToInt(c.DefaultPostForm("pageNumber", "1"))     // 当前页
	customerType := util.ToInt(c.DefaultPostForm("customerType", "0")) // 客户类型
	customerList, count, err := customer.GetCustomerList(name, provinceId, cityId, pageSize, pageNumber, customerType)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005007, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(gin.H{
		"list":  customerList,
		"count": count,
	}))
}

// CreateCustomerInfo
// @Description: 创建客户信息
// @param c
func CreateCustomerInfo(c *gin.Context) {
	name := c.DefaultPostForm("name", "")                              // 客户名称
	provinceId := util.ToInt(c.DefaultPostForm("provinceId", "0"))     // 客户所属省份ID
	cityId := util.ToInt(c.DefaultPostForm("cityId", "0"))             // 客户所属城市ID
	address := c.DefaultPostForm("address", "")                        // 客户具体地址
	gender := util.ToInt(c.DefaultPostForm("gender", "0"))             // 客户性别，1男2女
	customerType := util.ToInt(c.DefaultPostForm("customerType", "0")) // 客户类型
	description := c.DefaultPostForm("description", "")                // 描述
	if provinceId == 0 || cityId == 0 || gender == 0 || customerType == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005001, "请求参数不能为空"))
		return
	}
	if err := customer.CreateCustomerInfo(customer.Customer{
		Name:         name,
		RecordTime:   time.Now().Unix(), // 建档时间
		Gender:       gender,
		CustomerType: customerType,
		ProvinceID:   provinceId,
		CityID:       cityId,
		Address:      address,
		Description:  description,
	}); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005007, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(nil))
}

// UpdateCustomerInfo
// @Description: 修改指定客户信息
// @param c
func UpdateCustomerInfo(c *gin.Context) {
	customerID := util.ToInt(c.DefaultPostForm("customerId", "0"))
	name := c.DefaultPostForm("name", "")                          // 客户名称
	provinceId := util.ToInt(c.DefaultPostForm("provinceId", "0")) // 客户所属省份ID
	cityId := util.ToInt(c.DefaultPostForm("cityId", "0"))         // 客户所属城市ID
	address := c.DefaultPostForm("address", "")                    // 客户具体地址
	gender := util.ToInt(c.DefaultPostForm("gender", "0"))         // 客户性别，1男2女
	description := c.DefaultPostForm("description", "")            // 描述
	if customerID == 0 || provinceId == 0 || cityId == 0 || gender == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005001, "请求参数不能为空"))
		return
	}
	if err := customer.UpdateCustomerInfo(customerID, name, address, description, gender, provinceId, cityId); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005009, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(nil))
}

// DelCustomerInfo
// @Description: 删除指定客户信息
// @param c
func DelCustomerInfo(c *gin.Context) {
	customerID := util.ToInt(c.DefaultPostForm("customerId", "0"))
	if customerID == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005001, "请求参数不能为空"))
		return
	}
	if err := customer.DelCustomerInfo(customerID); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(910005009, err.Error()))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(nil))
}
